package com.jin10.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jin10.annotation.TitleType;
import com.jin10.bean.EconomicsResponse;
import com.jin10.constant.TitleIndicatorId;
import com.jin10.dto.BaseArrangeDto;
import com.jin10.dto.ParagraphDto;
import com.jin10.service.ISentenceLibraryService;
import com.jin10.service.ITitleHandlerService;
import com.jin10.util.KeyWordReplace;
import com.jin10.util.Paragraph;
import com.jin10.vo.TagTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author raofy
 * @date 2022-02-09 17:13
 * @desc 美国非农失业率数据公布处理
 */
@Service
@TitleType(value = TitleIndicatorId.US_NON_FARM_UNEMPLOYMENT_RATE)
public class USNonFarmUnemploymentRateHandler implements ITitleHandlerService {

    private final static String HIGHER_THAN_EXPECTED = "失业率：%s失率数为%s%%，高于市场预期的%s%%，%s前值%s%%； %s\n";
    private final static String LOWER_THAN_EXPECTED = "失业率：%s失率数为%s%%，低于市场预期的%s%%，%s前值%s%%； %s\n";
    private final static String BASICALLY_IN_LINE_EXPECTED = "失业率：%s月失率数为%s%%，基本符合市场预期的%s%%，%s前值%s%%； %s\n";
    private final static String JOB_LESS_RATE_RESULT = "%s%s%s，前值%s%s，预测值%s%s，实际值%s%s\n";

    @Autowired
    ISentenceLibraryService iSentenceLibraryService;

    @Override
    public String doAction(EconomicsResponse economicsResponse, int year, String month, String day, BaseArrangeDto dto) {
        if (Objects.nonNull(economicsResponse.getActual())) {
            return getJoblessRate(economicsResponse,year, month, day);
        } else {
            return "";
        }
    }

    @Override
    public String doAction(EconomicsResponse economicsResponse, int year, String month, String day) {
        return null;
    }

    /**
     * 失业率比较
     *
     * @param economicsResponse
     * @return
     */
    public String getJoblessRate(EconomicsResponse economicsResponse, int year, String month, String day) {
        StringBuffer stringBuffer = new StringBuffer();
        // 获取当前非农数据的公布值
        Float actual = Float.valueOf(economicsResponse.getActual().toString());
        // 预期值与公布值比较
        Float consensus = Float.valueOf(economicsResponse.getConsensus().toString());
        // 前值
        Float previous = Float.valueOf(economicsResponse.getPrevious());
        if (actual > consensus) {
            ParagraphDto paragraphDto = new ParagraphDto();
            paragraphDto.setTagIds(Arrays.asList(22));
            List<TagTreeVo> sentenceLibraryByTag = (List<TagTreeVo>) iSentenceLibraryService.getSentenceByTag(paragraphDto);
            List<String> strings = Paragraph.returnValue(sentenceLibraryByTag, new StringBuffer());
//            stringBuffer.append(KeyWordReplace.returnValue(iSentenceLibraryService.getById(156).getSentence(), previous, actual, consensus) + "，");
            stringBuffer.append(String.format(HIGHER_THAN_EXPECTED, economicsResponse.getTimePeriod(), actual, consensus, previous > actual ? "低于" : "高于", economicsResponse.getPrevious(), strings.get(0)));

            return stringBuffer.toString();
        } else if (actual < consensus) {
            ParagraphDto paragraphDto = new ParagraphDto();
            paragraphDto.setTagIds(Arrays.asList(21));
            List<TagTreeVo> sentenceLibraryByTag = (List<TagTreeVo>) iSentenceLibraryService.getSentenceByTag(paragraphDto);
            List<String> strings = Paragraph.returnValue(sentenceLibraryByTag, new StringBuffer());
            stringBuffer.append(String.format(LOWER_THAN_EXPECTED, economicsResponse.getTimePeriod(), actual, consensus, previous > actual ? "低于" : "高于", economicsResponse.getPrevious(), strings.get(0)));
//            stringBuffer.append(KeyWordReplace.returnValue(iSentenceLibraryService.getById(157).getSentence(), previous, actual, consensus) + "，");
            return stringBuffer.toString();
        } else {
            ParagraphDto paragraphDto = new ParagraphDto();
            paragraphDto.setTagIds(Arrays.asList(23));
            List<TagTreeVo> sentenceLibraryByTag = (List<TagTreeVo>) iSentenceLibraryService.getSentenceByTag(paragraphDto);
            List<String> strings = Paragraph.returnValue(sentenceLibraryByTag, new StringBuffer());
            stringBuffer.append(String.format(BASICALLY_IN_LINE_EXPECTED, economicsResponse.getTimePeriod(), actual, consensus, previous >= actual ? "低于等于" : "高于", economicsResponse.getPrevious(), strings.get(0)));
        }
        return String.format(JOB_LESS_RATE_RESULT, economicsResponse.getCountry(), economicsResponse.getTimePeriod(), economicsResponse.getName(), economicsResponse.getPrevious(), economicsResponse.getUnit(), economicsResponse.getConsensus(), economicsResponse.getUnit(), economicsResponse.getActual(), economicsResponse.getUnit());
    }
}
